<?php
include_once 'head.php';

//Patient Controller 
if( $_SERVER['REQUEST_METHOD'] == 'POST'){
    //Search Patients 
    $patientHandler = new PatientHandler();
    if(isset($_POST['trace'] )){
       $showResult = true ;
       try{
        $searchResult        = $patientHandler->searchPatients($_POST);
        $searchCount         = count( $searchResult );
       }catch( Exception $e){
           $msg = $e->getMessage();
           $showResult = false ;
       }
       
    }
    //Add Patients 
    elseif( isset($_POST['add']) ){
       
        $patientInfo            = array();
        $patientInfo['fname']   = $_POST['fname'];
        $patientInfo['mname']   = $_POST['mname'];
        $patientInfo['sname']   = $_POST['sname'];
        $patientInfo['sex']     = $_POST['sex'];
        $patientInfo['yy']      = sprintf("%02d",$_POST['yy']);
        $patientInfo['mm']      = sprintf("%02d",$_POST['mm']);
        $patientInfo['dd']      = sprintf("%02d",$_POST['dd']);
        $patientInfo['dob']      = $patientInfo['yy'].'-'.$patientInfo['mm'].'-'.$patientInfo['dd'];
        
        $patientHandler->savePatient( $patientInfo );
        header("Location:patient-personal.php");
    }
}
?>


<table class="tg">
  <tr><th class="thg">Find Patient(s):</th></tr>
  <tr><th class="tdw">
        <div id="invalid" ></div>
        <?php if(isset($msg)){ ?>
            <div id="message" class="message error" >
                <?php echo $msg ?>
            </div>
        <?php } ?>
      </th>
  </tr>
  <tr><td class="tdw">
    <form action="ptfc.php" method="post" id="pt_search">
  	<table cellpadding="10" width="80%" align="left">
  		<tr>

            <td><input name="name" id="name" class="searchbox"    value ="<?php if(isset($searchParms['name'])) echo $searchParms['name']; ?>" /></td>
            <td><input name="dob" id="dob" class="input"    value ="<?php if(isset($searchParms['dob'])) echo $searchParms['dob']; ?>" /></td>
   			<td><input type="submit" name="trace" value="Search Patient" class="button" /></td>
   		</tr>
   </table></form>
</td></tr>
  
  <tr>
      <td class="tdw">
          <table cellpadding="0" cellspacing="0" border="0"   id="dataTable" width="100%">
            
          </table>
      </td>
  </tr>

  
    
</table>
  

  <br /><br />
  <table class="tg">
  <tr><th class="thg">Create Patient:</th></tr>
  <tr><td class="tdw"><div><ul id="invalid2"></ul></div></td></tr>
  <tr><td class="tdw">
    <form action="ptfc.php" method="post" id="pt_create">
    <table cellpadding="10"  align="left">
      <tr>
        <td>First Name:<font color="red">*</font><br /><input name="fname" type="text" size="10" maxlength="20" class="input required" /><br /></td>
        <td>Middle Name:<br /><input name="mname" type="text" size="10" maxlength="20" class="input" /><br /></td>
        <td>Surname:<br /><input name="sname" type="text" size="10" maxlength="20" class="input" /><br /></td>
        <td>Sex:<font color="red">*</font><br /><input name="sex" type="text" size="2" maxlength="1" value ="" class="input required sexM"/><br /></td>
        <td>Birth date:<font color="red">*</font><br /><input name="yy" type="text" size="2" maxlength="2" value ="" class="required" />/<input name="mm" type="text" size="2" maxlength="2" value ="" class="required" />/<input name="dd" type="text" size="2" maxlength="2" value ="" class="required dob_day" />(yy/mm/dd) eg:12/01/01<br /></td>
        <td><br /><input type="submit" name="add" value="Create Patient" class="button" /><br /></td>
      </tr>
      </table></form>
       
    </td></tr>
</table>
  

<link href="assets/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
  <script language="javascript" src="assets/js/jquery.textbox-hinter.min.js"></script>
  
  <script language="javascript" src="assets/js/jquery.dataTables.min.js"></script>
	<script>
        var searchNameHint = 'Enter Patient Name';
        var searchDobHint = 'Enter Patient Date of Birth eg: 1995-06-04';
        
        function loadPatientResult(){
            var searchText = '';
            var dobParam = '';
            var dob = new Date($("#dob" ).val());
            var year = '';
            var month = '';
            var date = '';
            
            if($("#name" ).val() != searchNameHint){
                var searchText = $("#name" ).val();
            }
            if( $("#dob" ).val() != searchDobHint && !isNaN( dob.getTime())){
                dobParam = $("#dob" ).val();
                
            }
            
            
                 if(searchText.length >= 3 || dobParam != ''){
                     
                     if(dobParam != ''){
                         year = dob.getFullYear();
                         month = dob.getMonth();
                         date= dob.getDate();
                     }
                     $('#dataTable').show();
                    $('#dataTable').dataTable({
                       "bServerSide": true,
                       "bDestroy": true, 
                       "bFilter": false,
                       "bSort": false,
                       "bPaginate": false,
                       "sAjaxSource": "ajax-search-patient.php?name="+searchText+"&yy="+year+"&mm="+month+"&dd="+date,
                       "aoColumns": [
                            { "sTitle": "ID", "sType": "html"  },
                            { "sTitle": "Name", "sType": "html"  },
                            { "sTitle": "Sex" },
                            { "sTitle": "Date Of Birth" }
                       ]
                      });
                 }

        }
        
		$(document).ready(function(){
            
             $("#name" ).keyup(function(event) {
                     loadPatientResult();
                 });
             
              $("#dob" ).keyup(function(event) {
                     loadPatientResult();
                 });
                 
             $("#pt_search").submit(function() {
                 loadPatientResult();
                 return false;
             });
                
                 
             
           /* $( "#name" ).autocomplete({
                source: function( request, response ) {
                    $.ajax({
                        type: 'GET',
                        url: 'ajax-search-patient.php',
                        data: {
                            name: request.term
                        },
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function(data){
                                response(data);
                                
                        }
                    });
                },
                minLength: 3
            }); */
            
            //Search Box Hint
            $('#name').tbHinter({
                text: searchNameHint,
                class: 'hintText'
            });
            
            $('#dob').tbHinter({
                text: searchDobHint,
                class: 'hintText'
            });

		
            //Validation for Search box
            $("#pt_search").validate({

                rules: {
                    'name' : {
                        minlength: 3,
                        maxlength: 50
                    }
                    
                },
                messages: {

                    'name' : {
                        minlength: 'Please enter more than 3 letters',
                        maxlength: 'Please enter less than 50 letters'
                    },
                    'dob' :{
                        date: 'Please enter valid date'
                    }
                },
                errorElement: "div",
                wrapper: "",  // a wrapper around the error message
                errorPlacement: function(error, element) {

                     error.appendTo('#invalid');
                }
            });
        
            //Validation for create patient
            $('#pt_create').validate({
                rules: {
                    fname: "required",
                    sex: {
                        required: true,
                        sexM: true
                    },
                    yy: "required",
                    mm: "required",
                    dd: "required"

                },
                messages: {
                    fname: "Enter firstname, it's a required field",
                    sex: {
                        required: "Enter Sex(M or F), it's a required field",
                        sexM: "Please enter either F for Female or M for Male"
                    },
                    yy: "Enter Year of Birth, it's a required field",
                    mm: "Enter Month of Birth, it's a required field",
                    dd: "Enter Day of Birth, it's a required field"

                },

                errorLabelContainer: "#invalid2",
                wrapper: "li",

            });
	
});
	</script>
<?php
  include_once 'foot.php';
 ?>